package org.ziky.common.framework.wicket.behavior;

import java.io.Serializable;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.lang.Args;
import org.ziky.common.util.string.StrUtil;

public class FwAttributeModifier extends AttributeAppender {
    private static final long serialVersionUID = 1L;

    public FwAttributeModifier(String attribute, IModel<?> replaceModel) {
        super(attribute, replaceModel);
    }

    public FwAttributeModifier(String attribute, Serializable value) {
        super(attribute, value);
    }

    public FwAttributeModifier(String attribute, Serializable value, String separator) {
        super(attribute, value);
        setSeparator(separator);
    }

    public String getValue() {
        return StrUtil.parseStr(getReplaceModel());
    }

    public static FwAttributeModifier append(String attributeName, IModel<?> value) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, value, " ");
    }

    public static FwAttributeModifier append(String attributeName, Serializable value) {
        return append(attributeName, Model.of(value));
    }

    public static FwAttributeModifier prepend(String attributeName, IModel<?> value) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, value, " ") {
            private static final long serialVersionUID = 1L;

            protected String newValue(String currentValue, String replacementValue) {
                return super.newValue(replacementValue, currentValue);
            }
        };
    }

    public static FwAttributeModifier prepend(String attributeName, Serializable value) {
        return prepend(attributeName, Model.of(value));
    }

    public static FwAttributeModifier replace(String attributeName, IModel<?> value) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, value) {
            private static final long serialVersionUID = 1L;

            protected String newValue(String currentValue, String replacementValue) {
                return replacementValue;
            }
        };
    }

    public static FwAttributeModifier replace(String attributeName, Serializable value) {
        return replace(attributeName, Model.of(value));
    }

    public static FwAttributeModifier remove(String attributeName) {
        Args.notEmpty(attributeName, "attributeName");
        return replace(attributeName, Model.of(VALUELESS_ATTRIBUTE_REMOVE));
    }
}
